package com.mall.mymall.controller;


import com.mall.mymall.common.Result;
import com.mall.mymall.dao.dto.ProductDto;
import com.mall.mymall.dao.dto.QueryDto;
import com.mall.mymall.dao.dto.querypage.ProductPageRequest;
import com.mall.mymall.dao.entity.Products;
import com.mall.mymall.enums.Roles;
import com.mall.mymall.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 十里坡
 * @since 2025-03-06
 */
@RestController
@RequestMapping("/products")
public class ProductsController {

    @Autowired
    ProductsService productsService;

    /**
     * 分页查询商品信息
     * 包含：
     * productId 商品id 模糊
     * productName 商品名 模糊
     * storeId 门店id 模糊
     * categoryId 商品类别 动态条件
     *
     * @param productPageRequest
     * @return
     */
    @PostMapping("/pageQuery")
    public Result<QueryDto<ProductDto>> pageProducts(@RequestBody ProductPageRequest productPageRequest) {
        return Result.success(productsService.pageProducts(productPageRequest));
    }

    @PreAuthorize("hasRole('AREA_ADMIN_USER')")
    @PutMapping("/updateImage/{productId}")
    public Result<String> updateProductImage(@PathVariable Long productId, @RequestBody Map<String, String> requestBody) {
        String productImage = requestBody.get("productImage");
        try {
            productsService.updateProductImage(productId, productImage);
            return Result.success("图片更新成功");
        } catch (Exception e) {
            return Result.fail("图片更新失败");
        }
    }

    @PreAuthorize("hasRole('AREA_ADMIN_USER')")
    @PostMapping("/add")
    public Result<Products> addProduct(@RequestBody Products products) {
        try {
            productsService.save(products);
            return Result.success(products);
        } catch (Exception e) {
            return Result.fail("商品添加失败");
        }
    }

    @PreAuthorize("hasRole('AREA_ADMIN_USER')")
    @DeleteMapping("/delete/{productId}")
    public Result<String> deleteProduct(@PathVariable Long productId) {

        try {
            productsService.removeById(productId);
            return Result.success("商品删除成功");
        } catch (Exception e) {
            return Result.fail("商品删除失败");
        }
    }

    @PreAuthorize("hasRole('AREA_ADMIN_USER')")
    @PutMapping("/update/{productId}")
    public Result<String> updateProduct(@RequestBody Products products, @PathVariable Long productId) {
        try {
            productsService.updateById(products);
            return Result.success("商品更新成功");
        } catch (Exception e) {
            return Result.fail("商品更新失败");
        }
    }

    @GetMapping("/getAll")
    public Result<List<Products>> getAllProducts() {
        return Result.success(productsService.list());
    }

    /**
     * 假设是根据一个文本字段来匹配查询
     * @param keyword 表示用户输入的文本内容
     * @return 返回一个商品列表集合
     */
    @GetMapping("/searchProduct")
    public Result<List<Products>> SearchProducts(
            @RequestParam(required = false) String keyword) {
        return Result.success(productsService.SearchProducts(keyword));
    }

}

